#ifndef __CONFIG_INTERFACE_C
#define __CONFIG_INTERFACE_C

#include "config_interface.h"

int configInterface_GetConfig(char* ConfigName, char* InterfaceName, char *OptionName, char *pValue)
{
    char tmp[50] = {0};
    struct uci_ptr pUci_ptr;
    struct uci_context *uci_ctx = uci_alloc_context();
    if (uci_ctx == NULL)
    {
        PRINT_WARN("alloc uci context fail!\n");
        return -1;
    }

    sprintf(tmp, "%s.%s.%s", ConfigName, InterfaceName, OptionName);
    if(uci_lookup_ptr(uci_ctx, &pUci_ptr, tmp, true) != UCI_OK)
    {
        PRINT_WARN("look up uci_ptr fail! uci_ptr[%s]\n", tmp);
        return -1;
    }
    strcpy(pValue, pUci_ptr.o->v.string);
    uci_free_context(uci_ctx);
    return 0;
}

int configInterface_SetConfig(const char* ConfigName,const char* InterfaceName, const char *OptionName, const char *pValue)
{
    struct uci_ptr pUci_ptr;
    memset(&pUci_ptr, 0 , sizeof(pUci_ptr));
    struct uci_context *uci_ctx = uci_alloc_context();
    if (uci_ctx == NULL)
    {
        PRINT_WARN("alloc uci context fail!\n");
        return -1;
    }

    pUci_ptr.package = ConfigName;
    pUci_ptr.section = InterfaceName;
    pUci_ptr.option = OptionName;
    pUci_ptr.value = pValue;

    uci_set(uci_ctx, &pUci_ptr);
    uci_commit(uci_ctx, &(pUci_ptr.p), false);

    uci_unload(uci_ctx, pUci_ptr.p);
    uci_free_context(uci_ctx);
    return 0;
}

#endif
